package remind;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.*;
import javax.swing.text.rtf.RTFEditorKit;

public class remind extends javax.swing.JFrame {

    public static LinkedList<Note> noteList = new LinkedList<Note>();
    AbstractDocument doc;
    static final int MAX_CHARACTERS = 1024;
    private int edId=-1;

    public remind() {
        initComponents();
        initDocument();
        notesList.setAutoCreateRowSorter(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        textPane = new javax.swing.JTextPane();
        fontComboBox = new javax.swing.JComboBox();
        sizeComboBox = new javax.swing.JComboBox();
        colorButton = new javax.swing.JButton();
        openButton = new javax.swing.JButton();
        trans2eng = new javax.swing.JButton();
        exportButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        nameTField = new javax.swing.JTextField();
        saveButton = new javax.swing.JButton();
        trans2ukr = new javax.swing.JButton();
        bolButton = new javax.swing.JButton(new StyledEditorKit.BoldAction());
        italicButton = new javax.swing.JButton(new StyledEditorKit.ItalicAction());
        jSpinner1 = new javax.swing.JSpinner();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        notesList = new javax.swing.JTable(){     public boolean isCellEditable(int rowIndex, int vColIndex) {         return true;     } };
        jScrollPane3 = new javax.swing.JScrollPane();
        archiveList = new javax.swing.JTable(){     public boolean isCellEditable(int rowIndex, int vColIndex) {         return true;     } };
        priorComboBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        newMenu = new javax.swing.JMenuItem();
        importMenu = new javax.swing.JMenuItem();
        exitMenu = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        aboutMenu = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Remider");
        setMinimumSize(new java.awt.Dimension(930, 400));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        StyledDocument styledDoc = textPane.getStyledDocument();
        if (styledDoc instanceof AbstractDocument) {
            doc = (AbstractDocument) styledDoc;
            doc.setDocumentFilter(new Formatting(MAX_CHARACTERS));
        } else {
            System.err.println("Text pane's document isn't an AbstractDocument!");
            System.exit(-1);
        }
        textPane.setName(""); // NOI18N
        textPane.setCaretPosition(0);
        textPane.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
                textPaneCaretPositionChanged(evt);
            }
        });
        jScrollPane2.setViewportView(textPane);

        fontComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Arial", "Comic Sans MS", "Tahoma", "Times New Roman", "Verdana",}));
        fontComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fontComboBoxActionPerformed(evt);
            }
        });
        fontComboBox.setSelectedIndex(0);

        sizeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "8", "10", "12", "14", "16", "24", "36" }));
        sizeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sizeComboBoxActionPerformed(evt);
            }
        });

        colorButton.setText("Color");
        colorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colorButtonActionPerformed(evt);
            }
        });

        openButton.setText("Open ");
        openButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openButtonActionPerformed(evt);
            }
        });

        trans2eng.setText("Ukr-Eng");
        trans2eng.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                trans2engActionPerformed(evt);
            }
        });

        exportButton.setText("Export");
        exportButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exportButtonActionPerformed(evt);
            }
        });

        deleteButton.setText("Delete");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        nameTField.setText("Note name");
        nameTField.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                nameTFieldMousePressed(evt);
            }
        });
        nameTField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameTFieldActionPerformed(evt);
            }
        });

        saveButton.setText("Save");
        saveButton.setToolTipText("");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        trans2ukr.setText("Eng-Ukr");
        trans2ukr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                trans2ukrActionPerformed(evt);
            }
        });

        bolButton.setAction(new StyledEditorKit.BoldAction());
        bolButton.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        bolButton.setText("B");
        bolButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bolButtonActionPerformed(evt);
            }
        });

        italicButton.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        italicButton.setText("I");
        italicButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                italicButtonActionPerformed(evt);
            }
        });

        jSpinner1.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(), new java.util.Date(1325412060000L), null, java.util.Calendar.DAY_OF_MONTH));

        jTabbedPane1.setToolTipText("");
        jTabbedPane1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jTabbedPane1MousePressed(evt);
            }
        });

        notesList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "", "Name", "Date", "Priority", ""
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Boolean.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                true, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(notesList);
        notesList.getColumnModel().getColumn(0).setResizable(false);
        notesList.getColumnModel().getColumn(0).setPreferredWidth(20);
        notesList.getColumnModel().getColumn(3).setResizable(false);
        notesList.getColumnModel().getColumn(3).setPreferredWidth(30);
        notesList.getColumnModel().getColumn(4).setMinWidth(0);
        notesList.getColumnModel().getColumn(4).setPreferredWidth(0);
        notesList.getColumnModel().getColumn(4).setMaxWidth(0);

        jTabbedPane1.addTab("Active ", jScrollPane1);

        archiveList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "", "Name", "Date", "Priority", ""
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Boolean.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                true, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(archiveList);
        archiveList.getColumnModel().getColumn(0).setResizable(false);
        archiveList.getColumnModel().getColumn(0).setPreferredWidth(20);
        archiveList.getColumnModel().getColumn(3).setResizable(false);
        archiveList.getColumnModel().getColumn(3).setPreferredWidth(30);
        archiveList.getColumnModel().getColumn(4).setMinWidth(0);
        archiveList.getColumnModel().getColumn(4).setPreferredWidth(0);
        archiveList.getColumnModel().getColumn(4).setMaxWidth(0);

        jTabbedPane1.addTab("Archive", jScrollPane3);

        priorComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5" }));

        jLabel1.setText("Priority:");

        jMenu1.setText("File");

        newMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        newMenu.setText("New");
        newMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newMenuActionPerformed(evt);
            }
        });
        jMenu1.add(newMenu);

        importMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I, java.awt.event.InputEvent.CTRL_MASK));
        importMenu.setText("Load");
        jMenu1.add(importMenu);

        exitMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        exitMenu.setText("Exit");
        exitMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuActionPerformed(evt);
            }
        });
        jMenu1.add(exitMenu);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");

        aboutMenu.setText("About");
        jMenu2.add(aboutMenu);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        sizeComboBox.setSelectedIndex(2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(openButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(deleteButton)))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(saveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 221, Short.MAX_VALUE)
                        .addComponent(trans2eng)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(trans2ukr, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(exportButton))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(bolButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(italicButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(fontComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(sizeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(colorButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(priorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(nameTField, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 57, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(sizeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(colorButton)
                            .addComponent(bolButton)
                            .addComponent(italicButton)
                            .addComponent(fontComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(priorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nameTField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(exportButton)
                    .addComponent(trans2eng)
                    .addComponent(trans2ukr)
                    .addComponent(openButton)
                    .addComponent(deleteButton)
                    .addComponent(saveButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    protected JMenu createStyleMenu() {
        JMenu menu = new JMenu("Size");

        menu.add(new StyledEditorKit.ForegroundAction("Green",
                Color.green));
        menu.add(new StyledEditorKit.ForegroundAction("Blue",
                Color.blue));
        menu.add(new StyledEditorKit.ForegroundAction("Black",
                Color.black));

        return menu;
    }

private void exitMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuActionPerformed

    ImpOut save = new ImpOut();
    try {
        save.exportNotes(noteList);

        // TODO add your handling code here:
    } catch (IOException ex) {
        Logger.getLogger(remind.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.exit(0);
}//GEN-LAST:event_exitMenuActionPerformed

    private void initDocument() {
        String initString[] = {"Надрукуйте замітку тут."};

        SimpleAttributeSet[] attrs = initAttributes(initString.length);

        try {
            for (int i = 0; i < initString.length; i++) {
                doc.insertString(doc.getLength(), initString[i],
                        attrs[i]);
            }
        } catch (BadLocationException ble) {
            System.err.println("Couldn't insert initial text.");
        }
    }

    SimpleAttributeSet[] initAttributes(int length) {
        SimpleAttributeSet[] attrs = new SimpleAttributeSet[length];

        attrs[0] = new SimpleAttributeSet();
        StyleConstants.setFontFamily(attrs[0], "Arial");
        StyleConstants.setFontSize(attrs[0], 12);
        return attrs;
    }
private void trans2engActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_trans2engActionPerformed
    Translit tr = new Translit();
    textPane.setStyledDocument(tr.ukr2eng(textPane.getStyledDocument(), textPane.getSelectionStart(), textPane.getSelectionEnd()));
    textPane.setCaretPosition(0);
}//GEN-LAST:event_trans2engActionPerformed

private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openButtonActionPerformed
    if (jTabbedPane1.getSelectedIndex() == 0) {
        int bufa = (Integer) notesList.getValueAt(notesList.getSelectedRow(), 4);//get row id
        for (int i = 0; i < noteList.size(); i++) {
            if (bufa == noteList.get(i).getID()) {
                nameTField.setText(noteList.get(i).getName());
                textPane.setText(noteList.get(i).getText());
                jSpinner1.getModel().setValue(noteList.get(i).getDate());
                priorComboBox.setSelectedIndex(noteList.get(i).getPriority() - 1);
                edId=bufa;
            }
        }
    }
    if (jTabbedPane1.getSelectedIndex() == 1) {
        int buf = (Integer) archiveList.getValueAt(archiveList.getSelectedRow(), 4);
        for (int i = 0; i < noteList.size(); i++) {
            if (buf == noteList.get(i).getID()) {
                nameTField.setText(noteList.get(i).getName());
                textPane.setText(noteList.get(i).getText());
                jSpinner1.getModel().setValue(noteList.get(i).getDate());
                priorComboBox.setSelectedIndex(noteList.get(i).getPriority() - 1);
                edId=buf;
            }
        }
    }

}//GEN-LAST:event_openButtonActionPerformed

private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy  HH:mm");

    
    if(edId!=-1){
        for(int i=0;i<noteList.size();i++){
            if(edId==noteList.get(i).getID()){
                noteList.get(i).setName(nameTField.getText());
                noteList.get(i).setText(textPane.getText());
                noteList.get(i).setDate((Date) jSpinner1.getModel().getValue());
                String tmp = (String) priorComboBox.getSelectedItem();
                int pr = Integer.parseInt(tmp);
                noteList.get(i).setPriority(pr);
                edId=-1;
            }
        }
        
    }
    else{noteList.add(noteList.size(), new Note());
    //
    if (noteList.size() == 1) {
        noteList.get(noteList.size() - 1).setID(1);
    } else {
        noteList.get(noteList.size() - 1).setID(noteList.get(noteList.size() - 2).getID() + 1);
    }

    noteList.get(noteList.size() - 1).setName(nameTField.getText());
    noteList.get(noteList.size() - 1).setText(textPane.getText());
    noteList.get(noteList.size() - 1).setDate((Date) jSpinner1.getModel().getValue());
    String tmp = (String) priorComboBox.getSelectedItem();
    int pr = Integer.parseInt(tmp);
    noteList.get(noteList.size() - 1).setPriority(pr);} //else end
    actualTableReload();
    archiveTableReload();

}//GEN-LAST:event_saveButtonActionPerformed
    private void actualTableReload() {
        Archive.checkNote();
        //actualtable update
        for (int i = 0; i < notesList.getRowCount(); i++) {
            notesList.setValueAt(false, i, 0);
            notesList.setValueAt(null, i, 1);
            notesList.setValueAt(null, i, 2);
            notesList.setValueAt(null, i, 3);
            notesList.setValueAt(null, i, 4);
        }
        int freecell = 99;
        for (int i = 0; i < notesList.getRowCount(); i++) {
            if (notesList.getValueAt(i, 1) == null) {
                freecell = i;
                break;
            }
        }
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy  HH:mm");
        for (int i = 0; i < noteList.size(); i++) {
            if (noteList.get(i).getActuality() == true) {
                notesList.setValueAt(noteList.get(i).getName(), freecell, 1);
                notesList.setValueAt(dateFormat.format(noteList.get(i).getDate()), freecell, 2);
                notesList.setValueAt(noteList.get(i).getPriority(), freecell, 3);
                notesList.setValueAt(noteList.get(i).getID(), freecell, 4);
                freecell++;
            }
        }
    }

    private void archiveTableReload() {
        for (int i = 0; i < archiveList.getRowCount(); i++) {
            archiveList.setValueAt(false, i, 0);
            archiveList.setValueAt(null, i, 1);
            archiveList.setValueAt(null, i, 2);
            archiveList.setValueAt(null, i, 3);
            archiveList.setValueAt(null, i, 4);
        }
        int freecell = 99;
        for (int i = 0; i < archiveList.getRowCount(); i++) {
            if (archiveList.getValueAt(i, 1) == null) {
                freecell = i;
                break;
            }
        }
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy  HH:mm");
        for (int i = 0; i < noteList.size(); i++) {
            if (noteList.get(i).getActuality() == false) {
                archiveList.setValueAt(noteList.get(i).getName(), freecell, 1);
                archiveList.setValueAt(dateFormat.format(noteList.get(i).getDate()), freecell, 2);
                archiveList.setValueAt(noteList.get(i).getPriority(), freecell, 3);
                archiveList.setValueAt(noteList.get(i).getID(), freecell, 4);
                freecell++;
            }
        }
    }
private void trans2ukrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_trans2ukrActionPerformed
    Translit tr = new Translit();
    textPane.setStyledDocument(tr.eng2ukr(textPane.getStyledDocument(), textPane.getSelectionStart(), textPane.getSelectionEnd()));
    textPane.setCaretPosition(0);
}//GEN-LAST:event_trans2ukrActionPerformed

private void sizeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sizeComboBoxActionPerformed
    JComboBox box = (JComboBox) evt.getSource();
    String item = (String) box.getSelectedItem();
    int size = Integer.parseInt(item);
    new StyledEditorKit.FontSizeAction(item, size).actionPerformed(evt);
    textPane.setCaretPosition(0);
}//GEN-LAST:event_sizeComboBoxActionPerformed

private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
}//GEN-LAST:event_formWindowOpened

private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
    for (int i = 0; i < notesList.getRowCount(); i++) {
        if (notesList.getValueAt(i, 0) == null) {
            notesList.setValueAt(false, i, 0);
        }
    }
    for (int i = 0; i < archiveList.getRowCount(); i++) {
        if (archiveList.getValueAt(i, 0) == null) {
            archiveList.setValueAt(false, i, 0);
        }
    }

    for (int i = 0; i < (notesList.getRowCount()); i++) {
        if ((notesList.getValueAt(i, 0).equals(true))) {
            for (int j = 0; j < noteList.size(); j++) {
                if (noteList.get(j).getID() == notesList.getValueAt(i, 4)) {
                    DefaultTableModel model = (DefaultTableModel) notesList.getModel();
                    model.removeRow(i);
                    noteList.remove(j);
                    notesList.setModel(model);
                    i--;
                }
            }
        }
    }

    //deleteing from archive
    for (int i = 0; i < (archiveList.getRowCount()); i++) {
        if ((archiveList.getValueAt(i, 0).equals(true))) {
            for (int j = 0; j < noteList.size(); j++) {
                if (noteList.get(j).getID() == archiveList.getValueAt(i, 4)) {
                    DefaultTableModel model = (DefaultTableModel) archiveList.getModel();
                    model.removeRow(i);
                    noteList.remove(j);
                    archiveList.setModel(model);
                    i--;
                }
            }
        }
    }
}//GEN-LAST:event_deleteButtonActionPerformed

    private void fontComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fontComboBoxActionPerformed
        JComboBox box = (JComboBox) evt.getSource();
        String item = (String) box.getSelectedItem();
        new StyledEditorKit.FontFamilyAction(item, item).actionPerformed(evt);
    }//GEN-LAST:event_fontComboBoxActionPerformed

    private void nameTFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameTFieldActionPerformed
    }//GEN-LAST:event_nameTFieldActionPerformed

    private void newMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newMenuActionPerformed
    }//GEN-LAST:event_newMenuActionPerformed

    private void textPaneCaretPositionChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_textPaneCaretPositionChanged
        //doc.get;
    }//GEN-LAST:event_textPaneCaretPositionChanged

    private void colorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colorButtonActionPerformed

        final JDialog colorDialog = new JDialog();

        colorDialog.setModalityType(Dialog.ModalityType.MODELESS.APPLICATION_MODAL);
        colorDialog.setSize(650, 300);
        colorDialog.setUndecorated(true);
        colorDialog.setLocation(getX() + 100, getY() + 50);
        JPanel buttonPanel = new JPanel();


        final JColorChooser colorPicker = new javax.swing.JColorChooser();
        colorPicker.setColor(255, 255, 255);
        JButton colorOk = new JButton("Ok");
        JButton colorBack = new JButton("Back");
        colorOk.setSize(new Dimension(60, 25));
        colorBack.setSize(new Dimension(60, 25));
        buttonPanel.add(colorOk);
        buttonPanel.add(colorBack);
        colorDialog.setContentPane(colorPicker);
        colorDialog.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        colorPicker.setPreviewPanel(new JPanel());//preview panel remove
        colorPicker.setVisible(true);
        colorOk.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new StyledEditorKit.ForegroundAction(textPane.getSelectedText(), colorPicker.getColor()).actionPerformed(e);
                colorDialog.dispose();
                textPane.setCaretPosition(0);
            }
        });

        colorBack.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                colorDialog.setVisible(false);
            }
        });
        colorDialog.setVisible(true);
        colorDialog.pack();
        textPane.setCaretPosition(0);
    }//GEN-LAST:event_colorButtonActionPerformed

    private void jTabbedPane1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTabbedPane1MousePressed
        Archive.checkNote();
        archiveTableReload();
    }//GEN-LAST:event_jTabbedPane1MousePressed

    private void nameTFieldMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nameTFieldMousePressed
        nameTField.setText("");
    }//GEN-LAST:event_nameTFieldMousePressed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        ImpOut load = new ImpOut();
        try {
            noteList = load.getNotes();//#####


            actualTableReload();

            actualTableReload();

        } catch (FileNotFoundException ex) {
            Logger.getLogger(remind.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(remind.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(remind.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_formComponentShown

    private void italicButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_italicButtonActionPerformed
    }//GEN-LAST:event_italicButtonActionPerformed

    private void bolButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bolButtonActionPerformed
    }//GEN-LAST:event_bolButtonActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        ImpOut save = new ImpOut();
        try {
            save.exportNotes(noteList);

        } catch (IOException ex) {
            Logger.getLogger(remind.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_formWindowClosing

    private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportButtonActionPerformed
        if (textPane.getText().length() > 0) {

            JFileChooser chooser = new JFileChooser();
            chooser.setMultiSelectionEnabled(false);
            chooser.setAcceptAllFileFilterUsed(false);

            class MyTxtFilter extends javax.swing.filechooser.FileFilter {

                @Override
                public boolean accept(File file) {
                    return file.isDirectory() || file.getAbsolutePath().endsWith(".txt");
                }

                @Override
                public String getDescription() {
                    return "Text documents (*.txt)";
                }
            }
            class MyRtfFilter extends javax.swing.filechooser.FileFilter {

                @Override
                public boolean accept(File file) {
                    return file.isDirectory() || file.getAbsolutePath().endsWith(".rtf");
                }

                @Override
                public String getDescription() {
                    return "Rich Text Format (*.rtf)";
                }
            }

            chooser.setFileFilter(new MyTxtFilter());
            chooser.addChoosableFileFilter(new MyRtfFilter());
            chooser.setDialogTitle("Save to...");
            int option = chooser.showSaveDialog(this);

            if (option == JFileChooser.APPROVE_OPTION) {
                if (chooser.getFileFilter().getDescription().equals("Text documents (*.txt)")) {
                    String path = chooser.getSelectedFile().getAbsolutePath();
                    if (!path.endsWith(".txt")) {
                        path = path.concat(".txt");
                    }
                    File theFileToSave = new File(path);

                    if (theFileToSave.exists()) {
                        option = JOptionPane.showConfirmDialog(this, "Replace existing file?");
                        if (option == JOptionPane.YES_OPTION) {
                            try {
                                DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                                BufferedWriter outFile = new BufferedWriter(new FileWriter(theFileToSave));
                                outFile.write(nameTField.getText() + "\r\n" +dateFormat.format((Date) jSpinner1.getModel().getValue()) + "\r\n" + textPane.getText());
                                outFile.flush();
                                outFile.close();
                            } catch (FileNotFoundException e) {
                            } catch (IOException e) {
                            }
                        }
                    } else {
                        try {
                            BufferedWriter outFile = new BufferedWriter(new FileWriter(theFileToSave));
                            outFile.write(nameTField.getText() + "\n" + textPane.getText());
                            outFile.flush();
                            outFile.close();
                        } catch (FileNotFoundException e) {
                        } catch (IOException e) {
                        }
                    }
                } else {
                    StyledDocument tempdoc = textPane.getStyledDocument();
                    RTFEditorKit kit = new RTFEditorKit();
                    String path = chooser.getSelectedFile().getAbsolutePath();
                    if (!path.endsWith(".rtf")) {
                        path = path.concat(".rtf");
                    }
                    File theFileToSave = new File(path);
                    if (theFileToSave.exists()) {
                        option = JOptionPane.showConfirmDialog(this, "Replace existing file?");
                        if (option == JOptionPane.YES_OPTION) {
                            try {
                                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(theFileToSave));
                                kit.write(out, tempdoc, tempdoc.getStartPosition().getOffset(), tempdoc.getLength());
                                out.flush();
                                out.close();
                            } catch (FileNotFoundException e) {
                            } catch (IOException e) {
                            } catch (BadLocationException e) {
                            }
                        }
                    } else {
                        try {
                            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(theFileToSave));
                            kit.write(out, tempdoc, tempdoc.getStartPosition().getOffset(), tempdoc.getLength());
                            out.flush();
                            out.close();
                        } catch (FileNotFoundException e) {
                        } catch (IOException e) {
                        } catch (BadLocationException e) {
                        }
                    }
                }

            }
        }
    }//GEN-LAST:event_exportButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(remind.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(remind.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(remind.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(remind.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new remind().setVisible(true);

            }
        });


    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenu;
    private javax.swing.JTable archiveList;
    private javax.swing.JButton bolButton;
    private javax.swing.JButton colorButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JMenuItem exitMenu;
    private javax.swing.JButton exportButton;
    private javax.swing.JComboBox fontComboBox;
    private javax.swing.JMenuItem importMenu;
    private javax.swing.JButton italicButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField nameTField;
    private javax.swing.JMenuItem newMenu;
    private javax.swing.JTable notesList;
    private javax.swing.JButton openButton;
    private javax.swing.JComboBox priorComboBox;
    private javax.swing.JButton saveButton;
    private javax.swing.JComboBox sizeComboBox;
    private javax.swing.JTextPane textPane;
    private javax.swing.JButton trans2eng;
    private javax.swing.JButton trans2ukr;
    // End of variables declaration//GEN-END:variables
}
